package br.com.fogas.medicao.utils;

import br.com.fogas.database.entidade.Cliente;
import br.com.fogas.medicao.application.ApplicationFogas;
import br.com.fogas.utils.FogasUtility;
import br.com.fogas.utils.StringUtility;
import br.com.fogas.utils.exception.VersaoSistemaException;

public class MedicaoUtils {
	
	public static String nomeCliente(Cliente cliente) {
		return new StringBuilder()
		.append(StringUtility.removeZeroEsquerdaString(cliente.getTx_cod_cliente()))
		.append(" - ")
		.append(cliente.getTx_nome())
		.toString();
	} 
	
	public static void validaVersaoApp(String versaoSAP) throws VersaoSistemaException {
		String versaoApp = FogasUtility.android.getVersionApp(ApplicationFogas.getInstance().getContext());
			
		if(!StringUtility.isEmpty(versaoSAP) && !StringUtility.isEmpty(versaoApp)) {
			String[] split1 = versaoSAP.split("[.]");
			String[] split2 = versaoApp.split("[.]");
			
			if(split1.length == 3 && split2.length == 3) {
				versaoSAP = "";
				versaoApp = "";
				for (int i = 0; i < 3; i++) {
					versaoSAP += split1[i];
					versaoApp += split2[i];
				}
				int vSap = Integer.valueOf(versaoSAP);
				int vApp = Integer.valueOf(versaoApp);
				
				if(vApp < vSap) {
					throw new VersaoSistemaException("VERSAO_DESATUALIZADA");
				}
			} else {
				throw new VersaoSistemaException("VERSAO_FORA_PADRAO_1.0.0");
			}
		} else {
			throw new VersaoSistemaException("VERSAO_SAP_VAZIA");
		} 
	}

	public static String[] observacaoOcorrencia(String observacao) {
		observacao = StringUtility.trim(observacao); 
		
		int length = StringUtility.length(observacao);
		String obs1 = "";
		String obs2 = "";
		if(StringUtility.isNotEmpty(observacao)) {
			if(length <= 132) {
				obs1 = StringUtility.substring(observacao, 0, length);
			} else if(length > 132 && length <= 264) {
				obs1 = StringUtility.substring(observacao, 0, 132);
				obs2 = StringUtility.substring(observacao, 132, length);
			} else if(length > 264) {
				obs1 = StringUtility.substring(observacao, 0, 132);
				obs2 = StringUtility.substring(observacao, 132, 264);
			}
		}
		
		String[] obs = new String[] { obs1, obs2 };
		return obs;
	}
}
